
with ada.command_line; use ada.command_line;
with ada.text_io;      use ada.text_io;
with ada.containers;   use ada.containers;

with ada.containers.vectors;

procedure leet9_palindrome is
    package foo is new ada.containers.vectors
        (index_type => positive, element_type => integer);
    use foo;

    procedure print (v : in vector) is
    begin
        for e of v loop
            put(integer'image(e));
        end loop;
        new_line;
    end;

    function is_palindrome 
        (x : in integer) return boolean is
        v : vector;
        operand : integer := x;
        i : integer := 1;
    begin
        if x < 0 then
            return false;
        end if;
        if x < 10 then
            return true;
        end if;
        while operand > 0 loop
            v.append(operand rem 10);
            operand := (operand/10);
        end loop;
        print(v);
        --put_line(integer'image(v.element(0)));
        --put_line(integer'image(v.last_index));
        if v.first_element /= v.last_element then
            return false;
        end if;
        while i < ((v.last_index)/2) loop
            if v.element(v.first_index + i) 
            /= v.element(v.last_index - i) then
                return false;
            end if;
            i := i + 1;
        end loop;
        return true;
    end;

    procedure main (input : in integer) is 
        result : boolean := is_palindrome(input);
    begin
        if result = true then
            put_line(integer'image(input) & " is a palindrome.");
        end if;
    end;
begin
    put_line("This is leetcode challenge #9 - palindrome numbers.");
    put_line("https://leetcode.com/problems/palindrome-number/");
    if argument_count < 1 then 
        put_line("Requires one argument: a number.");
        return;
    end if;
    main(integer'value(argument(1)));
end;
